<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title>linux进程、线程与cpu的亲和性（affinity） - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="linux进程、线程与cpu的亲和性（affinity）">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content="linux进程、线程与cpu的亲和性（affinity） - 天地维杰网" />
    <meta name="description" content="CPU的亲和性， 就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器，也称为CPU关联性；再简单的点的描述就将制定的进程或线程绑定到相应的cpu上；在多核运行的机器上，每个CPU本身自己会有缓存，缓存着进程使用的信息，而进程可能会被OS调度到其他CPU上，如此，CPU cache命中率就低了，当绑定CPU后，程序就会一直在指定的cpu跑，不会由操作系统调度到其他CPU上，性能有一定的提高。 " />
     
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/affinity-of-linux-processes-threads-and-cpu/" itemprop="url">
        linux进程、线程与cpu的亲和性（affinity）
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="2022-09-28">
    2022-09-28
</time>
</span> 
      

  <span class="post-category" >
  &nbsp; | &nbsp;
  <span class="post-meta-item-icon">
    <i class="fa fa-folder-o"></i>
  </span>
  <span class="post-meta-item-text">分类：</span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/linux" itemprop="url" rel="index">
        <span itemprop="name">linux</span>
      </a>
      &nbsp; 
    </span>
  
</span>


      
 <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">转载</span>
</span>

       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">2829 字 ~6分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    
    <div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> 文章系转载，便于整理和分类，原文地址：https://www.cnblogs.com/wenqiang/p/6049978.html</code></pre></div>
    

    

<h2 id="一-什么是cpu亲和性-affinity"><strong>一、什么是cpu亲和性（affinity）</strong></h2>

<p>　　CPU的亲和性， 就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器，也称为CPU关联性；再简单的点的描述就将制定的进程或线程绑定到相应的cpu上；在多核运行的机器上，每个CPU本身自己会有缓存，缓存着进程使用的信息，而进程可能会被OS调度到其他CPU上，如此，CPU cache命中率就低了，当绑定CPU后，程序就会一直在指定的cpu跑，不会由操作系统调度到其他CPU上，性能有一定的提高。</p>

<p>　　<strong>软亲和性（affinity）:</strong>  就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器，Linux 内核进程调度器天生就具有被称为 <em>软 CPU 亲和性（affinity）</em> 的特性，这意味着进程通常不会在处理器之间频繁迁移。这种状态正是我们希望的，因为进程迁移的频率小就意味着产生的负载小。</p>

<p>　　<strong>硬亲和性（affinity）：</strong>简单来说就是利用linux内核提供给用户的API，强行将进程或者线程绑定到某一个指定的cpu核运行。</p>

<p>　　<strong>解释</strong>：在linux内核中，所有的进程都有一个相关的数据结构，称为 task_struct。这个结构非常重要，原因有很多；其中与 亲和性（affinity）相关度最高的是 cpus_allowed 位掩码。这个位掩码由 n 位组成，与系统中的 n 个逻辑处理器一一对应。 具有 4 个物理 CPU 的系统可以有 4 位。如果这些 CPU 都启用了超线程，那么这个系统就有一个 8 位的位掩码。 如果为给定的进程设置了给定的位，那么这个进程就可以在相关的 CPU 上运行。因此，如果一个进程可以在任何 CPU 上运行，并且能够根据需要在处理器之间进行迁移，那么位掩码就全是 1。实际上，这就是 Linux 中进程的缺省状态；（这部分内容在这个博客中有提到一点：<a href="http://www.cnblogs.com/wenqiang/p/4802619.html）">http://www.cnblogs.com/wenqiang/p/4802619.html）</a></p>

<p>　<strong>cpus_allowed用于控制进程可以在哪里处理器上运行</strong></p>

<ul>
<li><strong><code>sched_set_affinity()</code> （用来修改位掩码）</strong></li>
<li><strong><code>sched_get_affinity()</code> （用来查看当前的位掩码）</strong></li>
</ul>

<h2 id="二-进程与cpu的绑定"><strong>二、进程与cpu的绑定</strong></h2>

<p>　sched_setaffinity可以将某个进程绑定到一个特定的CPU。你比操作系统更了解自己的程序，为了避免调度器愚蠢的调度你的程序，或是为了在多线程程序中避免缓存失效造成的开销，你可能会希望这样做</p>

<p>　　在进行进程与cpu的绑定前，我们先了解编写程序需要准备的知识点</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#000">SCHED_SETAFFINITY</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">)</span>                                                                      <span style="color:#000">Linux</span> <span style="color:#000">Programmer</span><span style="color:#a40000">&#39;</span><span style="color:#000">s</span> <span style="color:#000">Manual</span>                                                                     <span style="color:#000">SCHED_SETAFFINITY</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">)</span>

<span style="color:#000">NAME</span>
       <span style="color:#000">sched_setaffinity</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">sched_getaffinity</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">set</span> <span style="color:#000">and</span> <span style="color:#000">get</span> <span style="color:#000">a</span> <span style="color:#000">process</span><span style="color:#a40000">&#39;</span><span style="color:#000">s</span> <span style="color:#000">CPU</span> <span style="color:#000">affinity</span> <span style="color:#000">mask</span>

<span style="color:#000">SYNOPSIS</span>
       <span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define _GNU_SOURCE             </span><span style="color:#8f5902;font-style:italic">/* See feature_test_macros(7) */</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>       <span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;sched.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>
       <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">sched_setaffinity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pid_t</span> <span style="color:#000">pid</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">cpusetsize</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
       <span style="color:#8f5902;font-style:italic">/*该函数设置进程为pid的这个进程,让它运行在mask所设定的CPU上.如果pid的值为0,
</span><span style="color:#8f5902;font-style:italic">        *则表示指定的是当前进程,使当前进程运行在mask所设定的那些CPU上.
</span><span style="color:#8f5902;font-style:italic">        *第二个参数cpusetsize是mask所指定的数的长度.通常设定为sizeof(cpu_set_t).
</span><span style="color:#8f5902;font-style:italic">        *如果当前pid所指定的进程此时没有运行在mask所指定的任意一个CPU上,
</span><span style="color:#8f5902;font-style:italic">        *则该指定的进程会从其它CPU上迁移到mask的指定的一个CPU上运行.*/</span>

       <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">sched_getaffinity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pid_t</span> <span style="color:#000">pid</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">cpusetsize</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
       <span style="color:#8f5902;font-style:italic">/*该函数获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中.
</span><span style="color:#8f5902;font-style:italic">        *即获得指定pid当前可以运行在哪些CPU上.
</span><span style="color:#8f5902;font-style:italic">        *同样,如果pid的值为0.也表示的是当前进程*/</span>

<span style="color:#000">RETURN</span> <span style="color:#000">VALUE</span>
       <span style="color:#000">On</span> <span style="color:#000">success</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">sched_setaffinity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">and</span> <span style="color:#000">sched_getaffinity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0.</span>  <span style="color:#000">On</span> <span style="color:#000">error</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span> <span style="color:#000">is</span> <span style="color:#000">returned</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">and</span> <span style="color:#000">errno</span> <span style="color:#000">is</span> <span style="color:#000">set</span> <span style="color:#000">appropriately</span><span style="color:#000;font-weight:bold">.</span></code></pre></div>
<p>设置cpu affinity还需要用到一下宏函数</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">CPU_ZERO</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">set</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#8f5902;font-style:italic">/*这个宏对 CPU 集 set 进行初始化，将其设置为空集。*/</span>
<span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">CPU_SET</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">cpu</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">set</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#8f5902;font-style:italic">/*这个宏将 指定的 cpu 加入 CPU 集 set 中*/</span>
<span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">CPU_CLR</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">cpu</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">set</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#8f5902;font-style:italic">/*这个宏将 指定的 cpu 从 CPU 集 set 中删除。*/</span>
<span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">CPU_ISSET</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">cpu</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">set</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#8f5902;font-style:italic">/*如果 cpu 是 CPU 集 set 的一员，这个宏就返回一个非零值（true），否则就返回零（false）。*/</span></code></pre></div>
<p>下面下一个具体的例子：将当前进程绑定到0、1、2、3号cpu上</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define _GNU_SOURCE</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;sched.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;stdio.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;string.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;stdlib.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;unistd.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;errno.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>
<span style="color:#8f5902;font-style:italic">/* sysconf( _SC_NPROCESSORS_CONF ) 查看cpu的个数；打印用%ld长整。
</span><span style="color:#8f5902;font-style:italic"> * sysconf( _SC_NPROCESSORS_ONLN ) 查看在使用的cpu个数；打印用%ld长整 */</span>
<span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">main</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">argc</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">argv</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">cpus</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">int</span>  <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">cpu_set_t</span> <span style="color:#000">mask</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">cpu_set_t</span> <span style="color:#000">get</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#000">cpus</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sysconf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">_SC_NPROCESSORS_CONF</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">printf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">cpus: %d</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cpus</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#000">CPU_ZERO</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>    <span style="color:#8f5902;font-style:italic">/* 初始化set集，将set置为空*/</span>
    <span style="color:#000">CPU_SET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>  <span style="color:#8f5902;font-style:italic">/* 依次将0、1、2、3号cpu加入到集合，前提是你的机器是多核处理器*/</span>
    <span style="color:#000">CPU_SET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">CPU_SET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">CPU_SET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/*设置cpu 亲和性（affinity）*/</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">sched_setaffinity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">printf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Set CPU affinity failue, ERROR:%s</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">strerror</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">errno</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000">usleep</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1000</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* 让当前的设置有足够时间生效*/</span>

    <span style="color:#8f5902;font-style:italic">/*查看当前进程的cpu 亲和性*/</span>
    <span style="color:#000">CPU_ZERO</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">sched_getaffinity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">printf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">get CPU affinity failue, ERROR:%s</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">strerror</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">errno</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#8f5902;font-style:italic">/*查看运行在当前进程的cpu*/</span>
    <span style="color:#204a87;font-weight:bold">for</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">cpus</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>

        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">CPU_ISSET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">i</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> <span style="color:#8f5902;font-style:italic">/*查看cpu i 是否在get 集合当中*/</span>
            <span style="color:#000">printf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">this process %d of running processor: %d</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">getpid</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">i</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000">sleep</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//让程序停在这儿，方便top命令查看
</span><span style="color:#8f5902;font-style:italic"></span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p><strong>运行结果如下：</strong></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#000;font-weight:bold">[</span><span style="color:#000">root</span><span style="color:#a40000">@</span><span style="color:#000">localhost</span> <span style="color:#000">test</span><span style="color:#000;font-weight:bold">]</span><span style="color:#a40000">#</span> <span style="color:#000">gcc</span> <span style="color:#000">test</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">c</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">o</span> <span style="color:#000">test</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">pthread</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#000">root</span><span style="color:#a40000">@</span><span style="color:#000">localhost</span> <span style="color:#000">test</span><span style="color:#000;font-weight:bold">]</span><span style="color:#a40000">#</span> <span style="color:#000;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000">test</span>
<span style="color:#f57900">cpus</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">24</span>
<span style="color:#000">this</span> <span style="color:#000">process</span> <span style="color:#0000cf;font-weight:bold">2848</span> <span style="color:#000">of</span> <span style="color:#000">running</span> <span style="color:#f57900">processor</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">0</span>
<span style="color:#000">this</span> <span style="color:#000">process</span> <span style="color:#0000cf;font-weight:bold">2848</span> <span style="color:#000">of</span> <span style="color:#000">running</span> <span style="color:#f57900">processor</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">1</span>
<span style="color:#000">this</span> <span style="color:#000">process</span> <span style="color:#0000cf;font-weight:bold">2848</span> <span style="color:#000">of</span> <span style="color:#000">running</span> <span style="color:#f57900">processor</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">2</span>
<span style="color:#000">this</span> <span style="color:#000">process</span> <span style="color:#0000cf;font-weight:bold">2848</span> <span style="color:#000">of</span> <span style="color:#000">running</span> <span style="color:#f57900">processor</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">3</span></code></pre></div>
<p><strong>上面代码当中用到了syscall这个函数，顺便也在这里做一下说明</strong></p>

<p>　　syscall是执行一个系统调用，根据指定的参数number和所有系统调用的接口来确定调用哪个系统调用，用于用户空间跟内核之间的数据交换</p>

<p>　　下面是syscall函数原型及一些常用的number　</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> 1 //syscall - indirect system call
 2 SYNOPSIS
 3        #define _GNU_SOURCE         /* See feature_test_macros(7) */
 4        #include &lt;unistd.h&gt;
 5        #include &lt;sys/syscall.h&gt;   /* For SYS_xxx definitions */
 6 
 7        int syscall(int number, ...);
 8        
 9 /* sysconf( _SC_PAGESIZE );  此宏查看缓存内存页面的大小；打印用%ld长整型。
10  sysconf( _SC_PHYS_PAGES ) 此宏查看内存的总页数；打印用%ld长整型。
11  sysconf( _SC_AVPHYS_PAGES ) 此宏查看可以利用的总页数；打印用%ld长整型。
12  sysconf( _SC_NPROCESSORS_CONF ) 查看cpu的个数；打印用%ld长整。
13  sysconf( _SC_NPROCESSORS_ONLN ) 查看在使用的cpu个数；打印用%ld长整。
14  (long long)sysconf(_SC_PAGESIZE) * (long long)sysconf(_SC_PHYS_PAGES) 计算内存大小。
15  sysconf( _SC_LOGIN_NAME_MAX ) 查看最大登录名长度；打印用%ld长整。
16  sysconf( _SC_HOST_NAME_MAX ) 查看最大主机长度；打印用%ld长整。
17  sysconf( _SC_OPEN_MAX )  每个进程运行时打开的文件数目；打印用%ld长整。
18  sysconf(_SC_CLK_TCK) 查看每秒中跑过的运算速率；打印用%ld长整。*/</code></pre></div>
<h2 id="三-线程与cpu的绑定"><strong>三、线程与cpu的绑定</strong></h2>

<p>线程于进程的绑定方法大体一致，需要注意的是线程绑定于进程的区别是所用函数不一样</p>

<p>线程绑定用到下面两个函数，跟进程类似就不做详细说明，下面直接贴出函数原型：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#000">NAME</span>
       <span style="color:#000">pthread_setaffinity_np</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">pthread_getaffinity_np</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">set</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000">get</span> <span style="color:#000">CPU</span> <span style="color:#000">affinity</span> <span style="color:#000">of</span> <span style="color:#000">a</span> <span style="color:#204a87;font-weight:bold">thread</span>

<span style="color:#000">SYNOPSIS</span>
       <span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define _GNU_SOURCE             </span><span style="color:#8f5902;font-style:italic">/* See feature_test_macros(7) */</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>       <span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;pthread.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>
       <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">pthread_setaffinity_np</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_t</span> <span style="color:#204a87;font-weight:bold">thread</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">cpusetsize</span><span style="color:#000;font-weight:bold">,</span>
                                  <span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">cpuset</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
       <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">pthread_getaffinity_np</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_t</span> <span style="color:#204a87;font-weight:bold">thread</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">cpusetsize</span><span style="color:#000;font-weight:bold">,</span>
                                  <span style="color:#000">cpu_set_t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">cpuset</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

       <span style="color:#000">Compile</span> <span style="color:#000">and</span> <span style="color:#000">link</span> <span style="color:#000">with</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">pthread</span><span style="color:#000;font-weight:bold">.</span>

<span style="color:#000">DESCRIPTION</span>
       <span style="color:#000">The</span>  <span style="color:#000">pthread_setaffinity_np</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span>  <span style="color:#000">function</span>  <span style="color:#000">sets</span> <span style="color:#000">the</span> <span style="color:#000">CPU</span> <span style="color:#000">affinity</span> <span style="color:#000">mask</span> <span style="color:#000">of</span> <span style="color:#000">the</span> <span style="color:#204a87;font-weight:bold">thread</span> <span style="color:#204a87;font-weight:bold">thread</span> <span style="color:#000">to</span> <span style="color:#000">the</span> <span style="color:#000">CPU</span> <span style="color:#000">set</span> <span style="color:#000">pointed</span> <span style="color:#000">to</span> <span style="color:#000">by</span> <span style="color:#000">cpuset</span><span style="color:#000;font-weight:bold">.</span>  <span style="color:#000">If</span> <span style="color:#000">the</span> <span style="color:#000">call</span> <span style="color:#000">is</span> <span style="color:#000">successful</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">and</span> <span style="color:#000">the</span> <span style="color:#204a87;font-weight:bold">thread</span> <span style="color:#000">is</span> <span style="color:#000">not</span>
       <span style="color:#000">currently</span> <span style="color:#000">running</span> <span style="color:#000">on</span> <span style="color:#000">one</span> <span style="color:#000">of</span> <span style="color:#000">the</span> <span style="color:#000">CPUs</span> <span style="color:#000">in</span> <span style="color:#000">cpuset</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">then</span> <span style="color:#000">it</span> <span style="color:#000">is</span> <span style="color:#000">migrated</span> <span style="color:#000">to</span> <span style="color:#000">one</span> <span style="color:#000">of</span> <span style="color:#000">those</span> <span style="color:#000">CPUs</span><span style="color:#000;font-weight:bold">.</span>

       <span style="color:#000">The</span> <span style="color:#000">pthread_getaffinity_np</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">function</span> <span style="color:#000">returns</span> <span style="color:#000">the</span> <span style="color:#000">CPU</span> <span style="color:#000">affinity</span> <span style="color:#000">mask</span> <span style="color:#000">of</span> <span style="color:#000">the</span> <span style="color:#204a87;font-weight:bold">thread</span> <span style="color:#204a87;font-weight:bold">thread</span> <span style="color:#000">in</span> <span style="color:#000">the</span> <span style="color:#000">buffer</span> <span style="color:#000">pointed</span> <span style="color:#000">to</span> <span style="color:#000">by</span> <span style="color:#000">cpuset</span><span style="color:#000;font-weight:bold">.</span>

       <span style="color:#000">For</span> <span style="color:#000">more</span> <span style="color:#000">details</span> <span style="color:#000">on</span> <span style="color:#000">CPU</span> <span style="color:#000">affinity</span> <span style="color:#000">masks</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">see</span> <span style="color:#000">sched_setaffinity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span>  <span style="color:#000">For</span> <span style="color:#000">a</span> <span style="color:#000">description</span> <span style="color:#000">of</span> <span style="color:#000">a</span> <span style="color:#000">set</span> <span style="color:#000">of</span> <span style="color:#000">macros</span> <span style="color:#000">that</span> <span style="color:#000">can</span> <span style="color:#000">be</span> <span style="color:#000">used</span> <span style="color:#000">to</span> <span style="color:#000">manipulate</span> <span style="color:#000">and</span> <span style="color:#000">inspect</span> <span style="color:#000">CPU</span> <span style="color:#000">sets</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">see</span> <span style="color:#000">CPU_SET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span>

       <span style="color:#000">The</span> <span style="color:#000">argument</span> <span style="color:#000">cpusetsize</span> <span style="color:#000">is</span> <span style="color:#000">the</span> <span style="color:#000">length</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">in</span> <span style="color:#000">bytes</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">of</span> <span style="color:#000">the</span> <span style="color:#000">buffer</span> <span style="color:#000">pointed</span> <span style="color:#000">to</span> <span style="color:#000">by</span> <span style="color:#000">cpuset</span><span style="color:#000;font-weight:bold">.</span>  <span style="color:#000">Typically</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">this</span> <span style="color:#000">argument</span> <span style="color:#000">would</span> <span style="color:#000">be</span> <span style="color:#000">specified</span> <span style="color:#000">as</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">cpu_set_t</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span>  <span style="color:#000;font-weight:bold">(</span><span style="color:#000">It</span> <span style="color:#000">may</span> <span style="color:#000">be</span> <span style="color:#000">some</span>  <span style="color:#000">other</span>
       <span style="color:#000">value</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">using</span> <span style="color:#000">the</span> <span style="color:#000">macros</span> <span style="color:#000">described</span> <span style="color:#000">in</span> <span style="color:#000">CPU_SET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000">dynamically</span> <span style="color:#000">allocating</span> <span style="color:#000">a</span> <span style="color:#000">CPU</span> <span style="color:#000">set</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">)</span>

<span style="color:#000">RETURN</span> <span style="color:#000">VALUE</span>
       <span style="color:#000">On</span> <span style="color:#000">success</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">these</span> <span style="color:#000">functions</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">on</span> <span style="color:#000">error</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">they</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">a</span> <span style="color:#000">nonzero</span> <span style="color:#000">error</span> <span style="color:#000">number</span></code></pre></div>
<p><strong>下面同样是个具体的例子：将当前线程绑定到0、1、2、3号cpu上</strong></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define _GNU_SOURCE</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;stdio.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;stdlib.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;string.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;unistd.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;pthread.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">include</span> <span style="color:#8f5902;font-style:italic">&lt;sched.h&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>
<span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">testfunc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">arg</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cpus</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">cpu_set_t</span> <span style="color:#000">mask</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">cpu_set_t</span> <span style="color:#000">get</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#000">cpus</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sysconf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">_SC_NPROCESSORS_CONF</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">printf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">this system has %d processor(s)</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cpus</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#000">CPU_ZERO</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">4</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> <span style="color:#8f5902;font-style:italic">/*将0、1、2、3添加到集合中*/</span>
        <span style="color:#000">CPU_SET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">i</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#8f5902;font-style:italic">/* 设置cpu 亲和性(affinity)*/</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_setaffinity_np</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_self</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">mask</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">fprintf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">stderr</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">set thread affinity failed</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#8f5902;font-style:italic">/* 查看cpu 亲和性(affinity)*/</span>
    <span style="color:#000">CPU_ZERO</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_getaffinity_np</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_self</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">fprintf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">stderr</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">get thread affinity failed</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#8f5902;font-style:italic">/* 查看当前线程所运行的所有cpu*/</span>
    <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">cpus</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">CPU_ISSET</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">i</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">printf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">this thread %d is running in processor %d</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">pthread_self</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">i</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000">sleep</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//查看
</span><span style="color:#8f5902;font-style:italic"></span>
    <span style="color:#000">pthread_exit</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>

<span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">main</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">argc</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">argv</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">pthread_t</span> <span style="color:#000">tid</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">tid</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">testfunc</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">fprintf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">stderr</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">thread create failed</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#000">pthread_join</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">tid</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p><strong>运行结果如下：</strong></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">[</span>root@localhost thread<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># gcc test.c -o test -pthread</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@localhost thread<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># ./test</span>
this system has <span style="color:#0000cf;font-weight:bold">24</span> processor<span style="color:#ce5c00;font-weight:bold">(</span>s<span style="color:#ce5c00;font-weight:bold">)</span>
this thread <span style="color:#0000cf;font-weight:bold">2812323584</span> is running in processor <span style="color:#0000cf;font-weight:bold">0</span>
this thread <span style="color:#0000cf;font-weight:bold">2812323584</span> is running in processor <span style="color:#0000cf;font-weight:bold">1</span>
this thread <span style="color:#0000cf;font-weight:bold">2812323584</span> is running in processor <span style="color:#0000cf;font-weight:bold">2</span>
this thread <span style="color:#0000cf;font-weight:bold">2812323584</span> is running in processor <span style="color:#0000cf;font-weight:bold">3</span></code></pre></div>
    </div>
    <footer class="post-footer">
     
 
<div class="post-tags">     
     
    <a href="http://www.shutdown.cn/tags/linux" rel="tag" title="linux">#linux#</a>
    
    <a href="http://www.shutdown.cn/tags/%e8%bf%9b%e7%a8%8b" rel="tag" title="进程">#进程#</a>
    
    <a href="http://www.shutdown.cn/tags/%e7%ba%bf%e7%a8%8b" rel="tag" title="线程">#线程#</a>
    
    <a href="http://www.shutdown.cn/tags/cpu" rel="tag" title="cpu">#cpu#</a>
    
    <a href="http://www.shutdown.cn/tags/%e4%ba%b2%e5%92%8c%e6%80%a7" rel="tag" title="亲和性">#亲和性#</a>
    
</div>



     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/cpu-affinity-in-linux/" rel="next" title="Linux中CPU亲和性（affinity）">
        <i class="fa fa-chevron-left"></i> Linux中CPU亲和性（affinity）
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/linux-socket-tcp-server-by-c/" rel="prev" title="linux下 socket tcp Server c语言编写（分别实现单进程，多进程，多线程）">
        linux下 socket tcp Server c语言编写（分别实现单进程，多进程，多线程） <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

  <ul class="sidebar-nav motion-element">
    <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
      文章目录
    </li>
    <li class="sidebar-nav-overview" data-target="site-overview">
      站点概览
    </li>
  </ul>

    <section class="site-overview sidebar-panel ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
    <div class="post-toc">
        <div class="post-toc-content"><nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#一-什么是cpu亲和性-affinity"><strong>一、什么是cpu亲和性（affinity）</strong></a></li>
<li><a href="#二-进程与cpu的绑定"><strong>二、进程与cpu的绑定</strong></a></li>
<li><a href="#三-线程与cpu的绑定"><strong>三、线程与cpu的绑定</strong></a></li>
</ul></li>
</ul>
</nav></div>
    </div>
</section>

  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>